package com.wedo.mall.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.e.library.dialog.EDialog;
import com.e.library.http.EApi;
import com.tencent.bugly.crashreport.CrashReport;
import com.wedo.mall.R;
import com.wedo.mall.entity.Agent;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.util.WUrl;

import java.util.HashMap;

import butterknife.OnClick;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.whatsapp.WhatsApp;

/**
 * 分享
 * Created by e on 2017/8/5.
 */

public class ShareDialog extends EDialog {

    public static final String SHARE_LOGO = "/sdcard/com.wedo.mall/logo.png";
    private OnekeyShare oks = new OnekeyShare();

    private String download = new StringBuilder().append(EApi.BASE_URL).append("download/download.html").toString();

    public static ShareDialog with(Context context) {
        return new ShareDialog(context);
    }

    public ShareDialog(Context context) {
        super(context);
        oks.disableSSOWhenAuthorize();
        //oks.setUrl(EApi.BASE_URL);
    }

    @Override
    protected int layout() {
        return R.layout.dialog_share;
    }

    @Override
    protected void onDialogCreated(Context context) {
    }

    public ShareDialog goods(Goods goods){

        StringBuilder sb = new StringBuilder();
        sb.append(EApi.BASE_URL).append("Goods/shareGoodsDetail/goods_id/").append(goods.id);
        String url = sb.toString();
        oks.setSilent(true);

        if (!TextUtils.isEmpty(goods.introduction)){
            if (goods.introduction.length() > 10240){
                String intro = goods.introduction.substring(10240);
                oks.setText(intro);
            }else {
                oks.setText(goods.introduction);
            }
        }

        oks.setTitle(goods.goods_name);
        String img = WUrl.url(goods.goods_image);
        oks.setUrl(url);//url仅在微信（包括好友和朋友圈）中使用
        oks.setSite("wedo");
        oks.setSiteUrl(EApi.BASE_URL);
        oks.setImageUrl(img);
        //Log.i("ShareDialog",img);
        Log.i("ShareDialog",url);
        return this;
    }

    public ShareDialog agent(Agent agent){
        oks.setSilent(true);
        oks.setText(agent.agent_name);
        String img = WUrl.url(agent.logo);
        oks.setUrl(download);
        oks.setSite("wedo1");
        oks.setTitleUrl(download);
        oks.setSiteUrl(download);
        oks.setImageUrl(img);
        return this;
    }

    /**分享app*/
    public ShareDialog app(){
        oks.setSilent(true);
        oks.setImagePath(SHARE_LOGO);
        oks.setSite(download);
        oks.setUrl(download);
        oks.setTitleUrl(download);
        oks.setSiteUrl(download);
        oks.setText(getContext().getResources().getString(R.string.share_app));

        return this;
    }

    @OnClick({R.id.iv_share_wechat, R.id.iv_share_pyq, R.id.iv_share_facebook, R.id.iv_share_whatsapp})
    protected void onShareClick(View view) {
        switch (view.getId()) {
            case R.id.iv_share_wechat:
                oks.setPlatform(Wechat.NAME);
                break;
            case R.id.iv_share_pyq:
                oks.setPlatform(WechatMoments.NAME);
                break;
            case R.id.iv_share_facebook:
                oks.setPlatform(Facebook.NAME);
                break;
            case R.id.iv_share_whatsapp:
                oks.setPlatform(WhatsApp.NAME);
                break;
        }

        //wechat();
        oks.show(getContext());
        dismiss();
    }


    @OnClick(R.id.ll_root)
    protected void onRootClick(){
        dismiss();
    }

    public void wechat() {
        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setTitle("标题");
        sp.setUrl("http://sharesdk.cn");
        sp.setText("我是共用的参数，这几个平台都有text参数要求，提取出来啦");
        //sp.setImageUrl("https://hmls.hfbank.com.cn/hfapp-api/9.png");
//        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setShareType(Platform.SHARE_TEXT);
        Platform weChat = ShareSDK.getPlatform (Wechat.NAME);
        // 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
        weChat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.d("ShareSDK", "onComplete ---->  分享成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                CrashReport.postCatchedException(throwable);
                Log.d("ShareSDK", "onError ---->  分享失败" + throwable.getStackTrace().toString());
                Log.d("ShareSDK", "onError ---->  分享失败" + throwable.getMessage());
                throwable.getMessage();
                throwable.printStackTrace();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.d("ShareSDK", "onCancel ---->  分享取消");
            }
        });

        weChat.share(sp);
    }

    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        //oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        //oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(getContext());
    }
}
